<?php
namespace app\beautiful\controller\admin\mall;
use app\BaseController;
use app\beautiful\model\logs\GoodsViewsModel;
use app\beautiful\model\order\GoodsOrderItemModel;
use app\beautiful\model\order\GoodsOrderModel;
use app\beautiful\model\order\StoreOrderModel;

class Statistics extends BaseController{
    public function getMallStatisticsCount()
    {
        $today_base_order_data=GoodsOrderModel::where([
            ['shop_id','=',$this->request->shopId],
            ['is_paid','=',1],
            ['status','>',1],
        ])->whereDay('add_time')->field('count(*) as count,sum(need_pay) as order_money')->find();

        $today_store_order_data=StoreOrderModel::where([
            ['shop_id','=',$this->request->shopId],
            ['is_paid','=',1],
            ['status','>',1],
        ])->whereDay('add_time')->field('count(*) as count,sum(need_pay) as order_money')->find();


        $total_base_order_data=GoodsOrderModel::where([
            ['shop_id','=',$this->request->shopId],
            ['is_paid','=',1],
            ['status','>',1],
        ])->field('count(*) as count,sum(need_pay) as order_money')->find();

        $total_store_order_data=StoreOrderModel::where([
            ['shop_id','=',$this->request->shopId],
            ['is_paid','=',1],
            ['status','>',1],
        ])->field('count(*) as count,sum(need_pay) as order_money')->find();

        $today_order_num=$today_base_order_data->count + $today_store_order_data->count;
         $today_order_money=round((($today_base_order_data->order_money === null ? 0 : $today_base_order_data->order_money *100) +
            ($today_store_order_data->order_money === null ? 0 : $today_store_order_data->order_money *100))/100,2);

        $total_order_num=$total_base_order_data->count + $total_store_order_data->count;
        $total_order_money=round((($total_base_order_data->order_money === null ? 0 : $total_base_order_data->order_money * 100) +
            ($total_store_order_data->order_money === null ? 0 : $total_store_order_data->order_money * 100))/100,2);

        $datas=[
            'today_order_num'=>$today_order_num,
            'today_order_money'=>$today_order_money,
            'total_order_num'=>$total_order_num,
            'total_order_money'=>$total_order_money,
        ];
        $return['datas']=$datas;

        return $this->success($return);
    }

    public function getMallStatisticsTakeCountRanking()
    {
        $time_type=$this->request->param('time_type/d',1);
        if(!in_array($time_type,[1,2])) $time_type=1;
        $order_where=[
            ['shop_id','=',$this->request->shopId],
            ['is_paid','=',1],
            ['status','>',1],
        ];
        $order_model= new GoodsOrderModel();
        $order_model=$order_model->where($order_where);
        if($time_type==1) $order_model=$order_model->whereMonth('add_time');
        if($time_type==2) $order_model=$order_model->whereYear('add_time');
        $sql=$order_model->field('order_id')->buildSql();

        
        $total=GoodsOrderItemModel::alias('a') ->join([$sql=> 'w'], 'a.order_id = w.order_id')->where([
            ['a.shop_id','=',$this->request->shopId]
        ])->field('sum(a.goods_num) as take_count,a.goods_id')->group('a.goods_id')->count();

        $list=GoodsOrderItemModel::alias('a') ->join([$sql=> 'w'], 'a.order_id = w.order_id')->with(['goods'])->where([
            ['a.shop_id','=',$this->request->shopId]
        ])->order(['take_count'=>'desc'])->field('sum(a.goods_num) as take_count,a.goods_id')->group('a.goods_id')
            ->limit($this->request->limit)
            ->page($this->request->page)
            ->select()
            ->toArray();
        foreach ($list as $k =>$v){
            $list[$k]['rank']=$this->request->limit * ($this->request->page - 1) + $k + 1;
        }

        $return=[
            'total'=>$total,
            'list'=>$list
        ];

        return $this->success($return);
    }

    public function getMallStatisticsViewsRanking()
    {
        $time_type=$this->request->param('time_type/d',1);
        if(!in_array($time_type,[1,2])) $time_type=1;
        $goods_views_model = new GoodsViewsModel();
        $where=[
            ['shop_id','=',$this->request->shopId],
        ];
        $goods_views_model=$goods_views_model->where($where);
        if($time_type==1) $goods_views_model=$goods_views_model->whereMonth('add_time');
        if($time_type==2) $goods_views_model=$goods_views_model->whereYear('add_time');
        $total=$goods_views_model->group('goods_id')->field('goods_id,count(goods_id) as view_count')->count();

        $list =$goods_views_model->with(['goods'])
            ->order(['view_count'=>'desc'])
            ->group('goods_id')
            ->field('goods_id,count(goods_id) as view_count')
            ->select()
            ->toArray();

        foreach ($list as $k =>$v){
            $list[$k]['rank']=$this->request->limit * ($this->request->page - 1) + $k + 1;
        }

        $return=[
            'total'=>$total,
            'list'=>$list
        ];

        return $this->success($return);
    }

    public function getMallStatisticsTrend()
    {
        $days=[];
        for($template=14;$template>0;$template--){
            $days[]=date('Y-m-d',strtotime('-'.$template.' days'));
        }
        $days[]=date("Y-m-d");
        $base_order_data=GoodsOrderModel::where([
            ['shop_id','=',$this->request->shopId],
            ['is_paid','=',1],
            ['status','>',1],
        ])->whereTime('add_time','-15 days')
            ->group("FROM_UNIXTIME(add_time,'%Y-%m-%d')")
            ->field("count(*) as count,sum(need_pay) as order_money,FROM_UNIXTIME(add_time,'%Y-%m-%d') as day")->select()->toArray();

        $store_order_data=StoreOrderModel::where([
            ['shop_id','=',$this->request->shopId],
            ['is_paid','=',1],
            ['status','>',1],
        ])->whereTime('add_time','-15 days')->group("FROM_UNIXTIME(add_time,'%Y-%m-%d')")
            ->field("count(*) as count,sum(need_pay) as order_money,FROM_UNIXTIME(add_time,'%Y-%m-%d') as day")->select()->toArray();

        $order_data=[];
        foreach ($days as $v){
            $item=[];
            $item['日期']=$v;
            foreach ($base_order_data as $key => $val){
                if($v==$val['day']){
                    $item['订单数']=$val['count'] ?? 0;
                    $item['订单金额']=$val['order_money'] ?? 0;
                }
            }

            foreach ($store_order_data as $key => $val){
                if($v==$val['day']){
                    $item['订单数']=$item['订单数'] + ($val['count'] ?? 0);
                    $item['订单金额']=$item['订单金额'] + ($val['order_money'] ?? 0);
                }
            }
            $item['订单数']=$item['订单数'] ?? 0;
            $item['订单金额']=$item['订单金额'] ?? 0;
            $order_data[]=$item;
        }
        $yesterday_order_data=GoodsOrderModel::where([
            ['shop_id','=',$this->request->shopId],
            ['is_paid','=',1],
            ['status','>',1],
        ])->whereDay('add_time','yesterday')->field('count(*) as count,sum(need_pay) as order_money')->find();

        $yesterday_store_order_data=StoreOrderModel::where([
            ['shop_id','=',$this->request->shopId],
            ['is_paid','=',1],
            ['status','>',1],
        ])->whereDay('add_time','yesterday')->field('count(*) as count,sum(need_pay) as order_money')->find();

        $total_order_data=GoodsOrderModel::where([
            ['shop_id','=',$this->request->shopId],
            ['is_paid','=',1],
            ['status','>',1],
        ])->field('count(*) as count,sum(need_pay) as order_money')->find();

        $total_store_order_data=StoreOrderModel::where([
            ['shop_id','=',$this->request->shopId],
            ['is_paid','=',1],
            ['status','>',1],
        ])->field('count(*) as count,sum(need_pay) as order_money')->find();

        $yesterday_order_num=$yesterday_order_data->count + $yesterday_store_order_data->count;
        $yesterday_order_money=round((($yesterday_order_data->order_money === null ? 0 : $yesterday_order_data->order_money *100) +
            ($yesterday_store_order_data->order_money === null ? 0 : $yesterday_store_order_data->order_money * 100))/100,2);

        $total_order_num=$total_order_data->count + $total_store_order_data->count;
        $total_order_money=round((($total_order_data->order_money === null ? 0 : $total_order_data->order_money * 100) +
            ($total_store_order_data->order_money === null ? 0 : $total_store_order_data->order_money * 100))/100,2);

        $order_total_data=[
            'yesterday_order_num'=>$yesterday_order_num,
            'yesterday_order_money'=>$yesterday_order_money,
            'total_order_num'=>$total_order_num,
            'total_order_money'=>$total_order_money,
        ];

        $return=[
            'order_data'=>$order_data,
            'order_total_data'=>$order_total_data
        ];
        return $this->success($return);
    }
}
